package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.doctor.entity.DoctorAssistantRelation;
import org.jeecg.common.modules.doctor.mapper.DoctorAssistantRelationMapper;
import org.jeecg.common.modules.doctor.service.IDoctorAssistantRelationService;
import org.jeecg.common.util.enums.StatusEnum;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: doctor_assistant_relation
 * @Author: jeecg-boot
 * @Date:   2020-08-15
 * @Version: V1.0
 */
@Service
public class DoctorAssistantRelationServiceImpl extends ServiceImpl<DoctorAssistantRelationMapper, DoctorAssistantRelation> implements IDoctorAssistantRelationService {

    @Override
    public boolean exists(String doctorId, String assistantId) {
        return Objects.nonNull(getRelation(doctorId,assistantId));
    }

    @Override
    public void saveDoctorAssistant(String doctorId, String assistantId, String assistantName) {
        DoctorAssistantRelation relation = new DoctorAssistantRelation();
        relation.setDoctorId(doctorId);
        relation.setAssistantId(assistantId);
        relation.setBackName(assistantName);
        relation.setCreateTime(new Date());
        relation.setUpdateTime(new Date());
        relation.setStatus(StatusEnum.VALID.getCode());
        save(relation);
    }

    @Override
    public void updateStatus(DoctorAssistantRelation exists, StatusEnum status) {
        exists.setStatus(status.getCode());
        exists.setUpdateTime(new Date());
        updateById(exists);
    }

    @Override
    public List<DoctorAssistantRelation> queryByDoctorId(String doctorId) {
        LambdaQueryWrapper<DoctorAssistantRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorAssistantRelation::getDoctorId,doctorId);
        query.eq(DoctorAssistantRelation::getStatus,StatusEnum.VALID.getCode());
        return list(query);
    }

    @Override
    public DoctorAssistantRelation getRelation(String doctorId,String assistantId){
        LambdaQueryWrapper<DoctorAssistantRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorAssistantRelation::getDoctorId,doctorId);
        query.eq(DoctorAssistantRelation::getAssistantId,assistantId);
        return getOne(query);
    }

    @Override
    public List<DoctorAssistantRelation> getRelation(String assistantId){
        LambdaQueryWrapper<DoctorAssistantRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorAssistantRelation::getAssistantId,assistantId);
        return list(query);
    }
}
